//files
AddCSLuaFile("cl_init.lua");
AddCSLuaFile("shared.lua");

include("shared.lua");

ENT.BananaType = 1;
ENT.Spawner = nil;

//when initializing
function ENT:Initialize()

	if self.BananaType == 1 then

		self.Entity:SetModel("models/props/cs_italy/bananna_bunch.mdl");

	else

		self.Entity:SetModel("models/props/cs_italy/bananna.mdl");

	end

	self.Entity:PhysicsInit(SOLID_VPHYSICS);
	self.Entity:SetMoveType(MOVETYPE_VPHYSICS);
	self.Entity:SetSolid(SOLID_VPHYSICS);

	//physics object
	local Physics = self.Entity:GetPhysicsObject();

	//if valid
	if ValidEntity(Physics) then

		//wake
		Physics:Wake();

	else

		self.Entity:Remove();

	end

end

function ENT:DoBananas()

	if self && ValidEntity(self.Entity) then
	
		local T = false;
	
		for I = 0,14 do

			local B = ents.Create("smod_banana");

			if ValidEntity(B) then

				B:SetPos(self.Entity:GetPos() + Vector(0,0,10) + VectorRand() * 5);
				B:SetAngles(Angle(math.Rand(0,360),math.Rand(0,360),math.Rand(0,360)));
				B.BananaType = 2;
				B.Spawner = self.Spawner
				B:Spawn();
				B:Activate();

				local Physics = B:GetPhysicsObject();

				if ValidEntity(Physics) then

					local Dir = VectorRand();

					Physics:ApplyForceCenter(Physics:GetMass() * Dir * math.Rand(4,8));
					
					timer.Simple(0.1 * I / 2,Physics.ApplyForceCenter,Physics,Vector(0,0,15));
					timer.Simple(0.15 * I / 2,B.Explode,B,T);
					
					T = !T;
					
				else

					B:Remove();

				end

			end

		end

		self.Entity:Remove();

	end
		
end

function ENT:Explode(T)

	if ValidEntity(self.Spawner) then

		if T then
	
			//effect data
			local FX = EffectData();
			FX:SetStart(self.Entity:GetPos());
			FX:SetOrigin(self.Entity:GetPos());
			FX:SetScale(1)

			//effect
			util.Effect("Explosion",FX)
		
			//damage
			util.BlastDamage(self.Entity,self.Spawner,self.Entity:GetPos(),230,90)
			
		end
		
	end
		
	//remove us
	self.Entity:Remove();

end 